/**
 * @param {string} pattern
 * @param {string} s
 * @return {boolean}
 */

// 双向映射→双哈希表/哈希+集合
var wordPattern = function (pattern, s) {
    const list = s.split(" ");

    /*
    let i = 0;
    const len = s.length;
    const list = [];
    while (i < len) {
        let str = '';
        while (s[i] !== ' ' && i < len) {
            str += s[i];
            i++;
        }
        list.push(str);
        i++;
    }
    */

    if (pattern.length !== list.length) {
        return false;
    }

    const dict_p = new Map();
    const dict_s = new Map();
    for (let i = 0; i < pattern.length; i++) {
        if (dict_p.has(pattern[i]) || dict_s.has(list[i])) {
            if (dict_p.get(pattern[i]) !== list[i] || dict_s.get(list[i]) !== pattern[i]) {
                return false;
            }
        } else {
            dict_p.set(pattern[i], list[i]);
            dict_s.set(list[i], pattern[i]);
        }
    }
    return true;
};